<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Categories model
 *
 * @package        925CMS
 * @subpackage    Categories Module
 * @category    Modules
 * @author        Phil Sturgeon - 925CMS Dev Team
 */
class Blog_categories_m extends MY_Model
{
    public function __construct()
    {
        parent::__construct();

        $this->_table = 'blog_categories';
    }

    /**
     * Insert a new category into the database
     * @access public
     * @param array $input The data to insert
     * @return string
     */
    public function insert($input = array())
    {
        $this->load->helper('text');
        parent::insert(array(
                            'title' => $input['title'],
                            'slug' => url_title(strtolower(convert_accented_characters($input['title'])))
                       ));

        return $input['title'];
    }

    /**
     * Update an existing category
     * @access public
     * @param int $id The ID of the category
     * @param array $input The data to update
     * @return bool
     */
    public function update($id, $input)
    {
        return parent::update($id, array(
                                        'title' => $input['title'],
                                        'slug' => url_title($input['title'])
                                   ));
    }
    /**
     * Callback method for validating the title
     * @access public
     * @param string $title The title to validate
     * @return mixed
     */
    public function check_title($title = '')
    {
        return parent::count_by('slug', url_title($title)) > 0;
    }

    /**
     * Insert a new category into the database via ajax
     * @access public
     * @param array $input The data to insert
     * @return int
     */
    public function insert_ajax($input = array())
    {
        $this->load->helper('text');
        return parent::insert(array(
                                   'title' => $input['title'],
                                   //is something wrong with convert_accented_characters?
                                   //'slug'=>url_title(strtolower(convert_accented_characters($input['title'])))
                                   'slug' => url_title($input['title'])
                              ));
    }

    /**
     * Build a multi-array of parent > children.
     *
     * @author Hoang Nguyen Quang
     * @access public
     * @param array $params
     *
     * @internal param string $group Either the group abbrev or the group id
     * @return array An array representing the link tree
     */
    public function get_category_tree($params = array())
    {
        if (!empty($params['order'])) {
            $this->db->order_by($params['order']);
        }
        else
        {
            $this->db->order_by('position');
        }
        $all_categories = $this->db->get($this->_table)->result_array();
        $categories = array();
        foreach ($all_categories as $row)
        {
            $categories[$row['id']] = $row;
        }

        $categoryarray = array();
        // build a multidimensional array of parent > children
        foreach ($categories AS $index => $row)
        {
            if ($row['parent'] != 0) {
                // add this link to the children array of the parent link
                $categories[$row['parent']]['children'][] =& $categories[$row['id']];
            }

            if (!isset($categories[$row['id']]['children'])) {
                $categories[$row['id']]['children'] = array();
            }

            // this is a root link
            if ($row['parent'] == 0) {
                $categoryarray[] =& $categories[$row['id']];
            }
        }
        return $categoryarray;
    }

    /**
     * Set the parent > child relations and child order
     *
     * @author Hoang Nguyen Quang
     * @param array $link
     * @return void
     */
    public function _set_children($link)
    {
        if (!empty($link['children'])) {
            foreach ($link['children'] as $i => $child)
            {
                $this->db->where('id', str_replace('category_', '', $child['id']));
                $this->db->update($this->_table, array('parent' => str_replace('category_', '', $link['id']), 'position' => $i));

                //repeat as long as there are children
                if ($child['children']) {
                    $this->_set_children($child);
                }
            }
        }
    }

    /**
     * Update category by condition
     *
     * @author Hoang Nguyen Quang
     * @access public
     * @param array $data
     * @return boolean
     */
    public function update_cat_by($data = array())
    {
        return $this->db->set($data)->update($this->_table);
    }
}